// A program to counter the number of different chars in a string.
// By Ben 04/10/2018

#include <iostream>
using namespace std;

class StringInfo{
private:
	int iUpper;
	int iLower;
	int iSpace;
	int iDigits;
	int iOther;
	int iWords;
	int iTotal;
	string sSource = "";

	int countChars(string source, char findchar){
		int cnt = 0;
		for (int i = 0; i < source.length(); i++){
			if (source[i] == findchar){
				cnt++;
			}
		}
		return cnt;
	}

	int countWords(string source){
		int i = 0;
		int j = 0;
		string buffer = "";
		string tmp = source;
		sSource = tmp;
		//Append last space so we cn get the end of the sting,
		if (tmp[tmp.length() - 1] != ' '){
			tmp.append(" ");
		}

		for (i = 0; i < tmp.length(); i++){
			//Check for a space
			if (isspace(tmp[i])){
				//INC counter
				j++;
				//Clear buffer
				buffer = "";
			}
			else{
				//Build buffer
				buffer += tmp[i];
			}
		}
		//Return counter
		return j;
	}
public:

	void Charfrequency(){
		int j = 0;
		int z = 0;
		int cLower = 0;
		int cUpper = 0;
		for (int i = 1; i < 26; i++){
			cLower = (97 + i);
			cUpper = (65 + i);
			j = this->countChars(sSource, (char)cLower);
			z = this->countChars(sSource, (char)cUpper);
			std::cout << (char)cLower << "=" << j << "\t" << (char)cUpper << "=" << z << endl;
		}
	}

	StringInfo(string source){
		int i = 0;
		iUpper = 0;
		iLower = 0;
		iSpace = 0;
		iDigits = 0;
		iOther = 0;
		iWords = 0;
		iTotal = source.length();

		iWords = countWords(source);

		while (i < source.length()){
			//Count uppercase and lowercase chars
			if (isalpha(source[i])){
				if (source[i] == toupper(source[i])){
					iUpper++;
				}
				if (source[i] == tolower(source[i])){
					iLower++;
				}
			}
			//Check for digits
			else if (isdigit(source[i])){
				//INC digit counter
				iDigits++;
			}
			//Check for space
			else if (isspace(source[i])){
				//INC space counter
				iSpace++;
			}
			else{
				//Count up other chars.
				iOther++;
			}
			//INC loop counter
			i++;
		}
	}

	int UpperCase(){
		return this->iUpper;
	}
	int LowerCase(){
		return this->iLower;
	}
	int Space(){
		return this->iSpace;
	}
	int Digits(){
		return this->iDigits;
	}
	int Alpha(){
		return this->iUpper + iLower;
	}
	int Other(){
		return this->iOther;
	}
	int Words(){
		return this->iWords;
	}
	int Total(){
		return this->iTotal;
	}
};

int main(){
	system("title Char Counter");
	//Count the different chars in the string
	string s0 = "C++ programming is a lot of fun AND cool. 123";
	StringInfo si(s0);
	std::cout << "Input string: " << s0.c_str() << endl << endl;
	//Output chars info
	std::cout << "Uppercase : " << si.UpperCase() << endl;
	std::cout << "Lowercase : " << si.LowerCase() << endl;
	std::cout << "Spaces    : " << si.Space() << endl;
	std::cout << "Digits    : " << si.Digits() << endl;
	std::cout << "Alpha     : " << si.Alpha() << endl;
	std::cout << "Other     : " << si.Other() << endl;
	std::cout << "Words     : " << si.Words() << endl;
	std::cout << "Total     : " << si.Total() << endl;
	std::cout << endl << "Char frequency" << endl;
	si.Charfrequency();
	system("pause");
	return 0;
}